package com.mysoft.gfyx.clue.backgroundjob;

import com.mysoft.framework.context.api.ContextManager;
import com.mysoft.gfyx.clue.remoteservice.RemoteProfitService;
import com.mysoft.gfyx.clue.service.CustomerPoolService;
import com.mysoft.gfyx.clue.service.SubscriptionService;
import com.mysoft.gfyx.clue.service.TenantPlatformProviderService;
import com.mysoft.gfyx.common.util.LogUtil;
import com.mysoft.sdk.tenantplatform.provider.TenantInfoDto;
import com.mysoft.sdk.tenantplatform.tianji.provider.TianjiTenantPlatformProvider;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@Component
public class SubscriptionJobHandler {

    @Autowired
    private SubscriptionService subscriptionService;

    @Autowired
    private CustomerPoolService customerPoolService;

    @Autowired
    private LogUtil logUtil;

    @Autowired
    private TianjiTenantPlatformProvider platformProvider;

    @Autowired
    private ContextManager contextManager;

    @Autowired
    private RemoteProfitService remoteProfitService;

    @XxlJob(value = "SubscriptionJobHandler", init = "init", destroy = "destroy")
    public void execute() {
        //获取租户信息
        List<TenantInfoDto> tenantInfoList = platformProvider.getAllTenantInfos();
        String enable = "";
        for (int i = 0; i < tenantInfoList.size(); i++) {
            if (tenantInfoList.get(i).getCode().equals(contextManager.fetchContextInfo().getTenantCode())) {
                enable = tenantInfoList.get(i).getEnabled();
            }
        }

        if (enable.equals("1")) {
            //1、订阅数据拉取
            try {
                logUtil.info("调度任务", "开始订阅数据拉取");
                subscriptionService.pull();
                logUtil.info("调度任务", "订阅数据拉取结束");
            } catch (Exception e) {
                logUtil.error("调度异常", e);
            }

            //2、更新主档案数据
            try {
                logUtil.info("调度任务", "开始更新主档案数据");
                customerPoolService.updateMainDeveloperData();
                logUtil.info("调度任务", "更新主档案数据结束");
            } catch (Exception e) {
                logUtil.error("调度异常", e);
            }

            //3、更新客户池云数据，因为启动太多job会有问题，所以更新客户池的调度也启动在这里
            try {
                logUtil.info("调度任务", "开始更新客户池云数据");
                customerPoolService.scheduleUpdatingCloudData();
                logUtil.info("调度任务", "客户池云数据更新结束");
            } catch (Exception e) {
                logUtil.error("调度异常", e);
            }

            // 4. 推送权益数据
            try {
                logUtil.info("调度任务", "开始推送权益数据");
                remoteProfitService.pushProfit();
                logUtil.info("调度任务", "推送权益数据结束");
            } catch (Exception e) {
                logUtil.error("调度异常-", e);
            }

            // 5. 更新云采购会员权益账号数量
            try {
                logUtil.info("调度任务", "开始更新云采购平台会员权益账号数量");
                remoteProfitService.pullYcgMemberProfit();
                logUtil.info("调度任务", "更新云采购平台会员权益账号数量结束");
            } catch (Exception e) {
                logUtil.error("调度异常-", e);
            }
        } else {
            logUtil.info("调度任务", "租户不存在或者停用，调度空跑");
        }

    }

    public void init() {
        System.out.println("已经初始化订阅调度！");
    }

    public void destroy() {
        System.out.println("已经销毁订阅调度……");
    }


}
